#pragma once

#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>

#include "Tools.h"
#include "../header/Constants.h"

typedef struct FileSystem FileSystem;

static int loadFromDisk(FileSystem *fs) {
    int fd = open("FileSystem", O_RDONLY);
    int load = False;
    if (fd != Error) {
        struct stat file_stat;
        int ret = fstat(fd, &file_stat);
        if (ret != Error) {
            if (file_stat.st_size == TOTAL_SIZE) {
                FILE *file = fopen("FileSystem", "rb");
                fread(fs, sizeof(char), TOTAL_SIZE, file);
                fclose(file);
                load = True;
            }
        }
        close(fd);
    }
    return load;
}

static Status saveToDisk(FileSystem *fs) {
    FILE *file = fopen("FileSystem", "wb");
    if (file == NULL) {
        return Error;
    }
    fwrite(fs, sizeof(char), TOTAL_SIZE, file);
    fclose(file);
    return Success;
}